package com.bootdo.common.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.IdUtil;
import com.bootdo.wx.dao.CmsContentDao;
import com.bootdo.wx.dao.MemberLevelChangeDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.dao.OrderTeamMoneyDao;
import com.bootdo.wx.domain.MemberLevelChangeDO;
import com.bootdo.wx.domain.MemberUserDO;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户保级  每月2号两点执行
 */
@Component
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
public class UserDownLevel implements Job{
	@Autowired
    private  MemberUserDao memberUserDao;
    @Autowired
    private OrderTeamMoneyDao orderTeamMoneyDao;
    @Autowired
    private CmsContentDao cmsContentDao;
    @Autowired
    private MemberLevelChangeDao memberLevelChangeDao;

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        TimeInterval timer = DateUtil.timer();
        if(DateUtil.dayOfMonth(new Date())!=2){
            System.out.println("不是2号,不能算业绩");
            return ;
        }
        //开始时间 上月2号开始时间
        Date strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(DateUtil.lastMonth()),1));
        //这个月1号的结束时间
        Date endDate=DateUtil.endOfDay(DateUtil.yesterday());
        //获取各个等级的保级业绩金额
        String tzMoney = cmsContentDao.getByType("money-tz-down").getValue();
        String hxMoney = cmsContentDao.getByType("money-hx-down").getValue();
        String hhrMoney = cmsContentDao.getByType("money-hhr-down").getValue();



        List<MemberUserDO> memberUserDOList = memberUserDao.getByLevelGt(0);
        if(memberUserDOList.size()>0){
            for (MemberUserDO memberUserDO : memberUserDOList) {
                Map<String,Object> map = new HashMap<>();
                map.put("userId",memberUserDO.getId());
                map.put("strDate",strDate);
                map.put("endDate",endDate);
                //获取完成的业绩金额
                String allMoney = "";
                int value = 0;
                int level = memberUserDO.getLevel();
                BigDecimal sumMoney = orderTeamMoneyDao.sumMoneyByUserId(map);
                if(level==1){
                    value = sumMoney.compareTo(new BigDecimal(tzMoney));
                    allMoney=tzMoney;
                }else if(level==2){
                    value = sumMoney.compareTo(new BigDecimal(hxMoney));
                    allMoney=hxMoney;
                }else if(level==3){
                    value = sumMoney.compareTo(new BigDecimal(hhrMoney));
                    allMoney=hhrMoney;
                }
                if(value<0){
                    //如果没有完成就降级
                    memberUserDO.setLevel(level-1);
                    memberUserDao.update(memberUserDO);
                    //添加等级变动记录
                    MemberLevelChangeDO memberLevelChangeDO = new MemberLevelChangeDO();
                    memberLevelChangeDO.setNewLevel(level-1);
                    memberLevelChangeDO.setOldLevel(level);
                    memberLevelChangeDO.setUserId(memberUserDO.getId());
                    memberLevelChangeDO.setId(IdUtil.simpleUUID());
                    memberLevelChangeDO.setComent("考核业绩不合格,完成业绩:"+sumMoney+",考核总业绩:"+allMoney);
                    memberLevelChangeDO.setCreateAt(new Date());
                    memberLevelChangeDO.setDelFlag(0);
                    memberLevelChangeDao.save(memberLevelChangeDO);
                }

            }
        }

        System.out.println("定时降级任务执行分钟数"+timer.intervalMinute());//花费分钟数

    }

}